﻿namespace DigitClassification
{
    public class DigitStats
    {
        public int Count;
        public int[,] CellDarkCount;

        public DigitStats(int dim)
        {
            CellDarkCount = new int[dim, dim];
        }

        /// <summary>
        /// return Pr(digit)
        /// </summary>
        /// <param name="total"></param>
        /// <param name="kSmoothing"></param>
        /// <returns></returns>
        public double GetProb(int total, int kSmoothing)
        {
            return CalcProbWithSmoothing(Count, total, kSmoothing, 10);
        }

        /// <summary>
        /// return Pr(f_ij=1 | C=digit) using Add-k smoothing
        /// </summary>
        /// <param name="j"></param>
        /// <param name="i"></param>
        /// /// <param name="kSmoothing"></param>
        /// <returns></returns>
        public double GetDarkCellProb(int i, int j, int kSmoothing)
        {
            return CalcProbWithSmoothing(CellDarkCount[i, j], Count, kSmoothing, 2);
        }

        /// <summary>
        /// Return probability according to Laplace smoothing:
        /// Pr_Lap(x) = (c(x)+k) / N + k * |X|
        /// </summary>
        /// <param name="count"></param>
        /// <param name="total"></param>
        /// <param name="kSmoothing"></param>
        /// <param name="sizeX"></param>
        /// <returns></returns>
        private static double CalcProbWithSmoothing(int count, int total, int kSmoothing, int sizeX)
        {
            return ((double)count + kSmoothing) / (total + sizeX * kSmoothing);
        }
    }
}